package com.party.web.web.controller.wechat;

import com.party.common.utils.StringUtils;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.wechat.WechatMessage;
import com.party.core.service.wechat.IWechatMessageService;
import com.party.core.service.wechatPlatform.IWechatOpenMemberService;
import com.party.web.utils.RealmUtils;
import com.party.web.web.dto.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Iterator;
import java.util.List;

/**
 * Created by admin on 2017/8/28.
 */
@Controller
@RequestMapping("/wechat/autoorattentionresponse")
public class AutoOrAttentionResponseController {

    private static Logger log = LoggerFactory.getLogger(AutoOrAttentionResponseController.class);

    @Autowired
    private IWechatMessageService iWechatMessageService;

    @Autowired
    private IWechatOpenMemberService wechatOpenMemberService;

    @RequestMapping("view")
    public ModelAndView AutoOrAttentionView(WechatMessage wechatMessage){
        //判断是否授权
        ModelAndView mv = null;
        MemberGroup member = RealmUtils.getCurrentUser();
        boolean isAuthorize = wechatOpenMemberService.isAuthorize(member.getId());
        if(isAuthorize){
            //已授权，跳转至正常页面
            mv = new ModelAndView("wechat/autoOrAttentionResponse");
            mv.addObject("type",wechatMessage.getType());
            //设置默认值 消息回复类型 text
            if(StringUtils.isEmpty(wechatMessage.getReplyType())){
                wechatMessage.setReplyType("text");
            }
            //设置memberid
            wechatMessage.setMemberId(member.getId());
            //当前状态：读取对应的回复消息,自动回复与关注回复有且只能有一条
            List<WechatMessage> list = iWechatMessageService.listPage(wechatMessage,null);
            Iterator<WechatMessage> iterator = list.iterator();
            if(iterator.hasNext()){
                mv.addObject("response",iterator.next());
            }
        }else {
            //未授权，跳转至授权绑定提示页
            mv = new ModelAndView("wechat/boundTips");
        }
        return mv;
    }

    @ResponseBody
    @RequestMapping("save")
    public AjaxResult save(WechatMessage wechatMessage){
        MemberGroup member = RealmUtils.getCurrentUser();
        boolean isAuthorize = wechatOpenMemberService.isAuthorize(member.getId());
        if(!isAuthorize){
            //未绑定，没权限进行操作
            return new AjaxResult(false);
        }
        if(StringUtils.isEmpty(wechatMessage.getId())){
            //新建回复
            wechatMessage.setMemberId(member.getId());
            iWechatMessageService.insert(wechatMessage);
            return new AjaxResult(true);
        }else{
            //更新回复信息
            iWechatMessageService.update(wechatMessage);
            return new AjaxResult(true);
        }
    }
}
